﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iMainAddin;
using System.IO;

namespace iClassAddin
{
    class iCreateClassData
    {
        public bool isInline;
        public bool isVirtual;
        public bool isMakeCpp;
        public String className;
        public String cppName;
        public String headerName;
        public String inlineName;
        public String parentName;
    }
    class iClassDocument : ExchangeDocument
    {
        public iClassDocument()
        {
        }

        public bool StartChange(String filePath, iCreateClassData createData)
        {
            if (filePath.Length < 0)
                return false;

            String fileString = "";
            bool isCpp = iSystem.iFileAssist.CheckExtension(filePath, "cpp");
            bool isHeader = iSystem.iFileAssist.CheckExtension(filePath, "h");
            using (StreamReader reader = new StreamReader(filePath))
            {
                String lineString = null;// reader.ReadLine();
                String lowerString = null;// lineString.ToLower();
                while (reader.Peek() >= 0)
                {
                    lineString = reader.ReadLine();
                    lowerString = lineString.ToLower();
                    if ( isHeader )
                    {
                        if (lowerString.Contains("~$(classname)(void)"))
                        {
                            if( createData.isVirtual == true  )
                                lineString = "\tvirtual" + lineString;
                            if (createData.isMakeCpp == false)
                            {
                                lineString.Remove(lineString.Length - 1, 1);
                                lineString += "{}";
                            }
                        }
                        else if (lowerString.Contains("$(classname)(void)"))
                        {
                            if (createData.isMakeCpp == false)
                            {
                                lineString.Remove(lineString.Length - 1, 1);
                                lineString += "{}";
                            }
                        }
                        else if (createData.isInline == true && lowerString.Contains("#endif"))
                        {
                            lineString = @"#include ""$(FileName).inl""" + "\n" + lineString;
                        }
                        else if (createData.parentName != null &&
                        lowerString.Contains("class $(projectnameup)_entry $(classname)"))
                        {
                            lineString = lineString + " : " + createData.parentName;
                        }
                    }
                    fileString += lineString+"\n";
                }
                reader.Close();
            }

            if (!ChangeTextVariables(ref fileString))
                return false;
            

            StreamWriter write = new StreamWriter(filePath);
            write.Flush();
            write.Write(fileString);
            write.Close();
            return true;

        }
    }
}
